package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.WxUserAddress;
import com.ruoyi.business.mapper.UserAddressMapper;
import com.ruoyi.business.service.IUserAddressService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 微信小程序用户收货地址Service业务层处理
 */
@Service
public class IUserAddressServiceImpl implements IUserAddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;

    /**
     * 查询用户收货地址列表
     * 
     * @param openid 微信用户openid
     * @return 用户收货地址
     */
    @Override
    public List<WxUserAddress> selectUserAddressList(String openid) {
        WxUserAddress userAddress = new WxUserAddress();
        userAddress.setOpenid(openid);
        return userAddressMapper.selectUserAddressList(userAddress);
    }

    /**
     * 查询用户收货地址详细信息
     * 
     * @param addressId 收货地址ID
     * @return 收货地址信息
     */
    @Override
    public WxUserAddress selectUserAddressById(Long addressId) {
        return userAddressMapper.selectUserAddressById(addressId);
    }

    /**
     * 查询用户默认收货地址
     * 
     * @param openid 微信用户openid
     * @return 默认收货地址
     */
    @Override
    public WxUserAddress selectDefaultUserAddress(String openid) {
        return userAddressMapper.selectDefaultUserAddress(openid);
    }

    /**
     * 新增用户收货地址
     * 
     * @param address 用户收货地址
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUserAddress(WxUserAddress address) {
        // 如果设置为默认地址，需要先将其他地址设置为非默认
        if ("1".equals(address.getIsDefault())) {
            userAddressMapper.updateDefaultAddress(address.getOpenid());
        }
        address.setCreateTime(DateUtils.getNowDate());
        address.setCreateBy(SecurityUtils.getUsername());
        address.setDelFlag("0");
        return userAddressMapper.insertUserAddress(address);
    }

    /**
     * 修改用户收货地址
     * 
     * @param address 用户收货地址
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUserAddress(WxUserAddress address) {
        // 如果设置为默认地址，需要先将其他地址设置为非默认
        if ("1".equals(address.getIsDefault())) {
            userAddressMapper.updateDefaultAddress(address.getOpenid());
        }
        address.setUpdateTime(DateUtils.getNowDate());
        address.setUpdateBy(SecurityUtils.getUsername());
        return userAddressMapper.updateUserAddress(address);
    }

    /**
     * 删除用户收货地址
     * 
     * @param addressId 用户收货地址主键
     * @return 结果
     */
    @Override
    public int deleteUserAddressById(Long addressId) {
        return userAddressMapper.deleteUserAddressById(addressId);
    }
    
    /**
     * 批量删除用户收货地址
     * 
     * @param addressIds 需要删除的地址ID
     * @return 结果
     */
    @Override
    public int deleteUserAddressByIds(Long[] addressIds) {
        return userAddressMapper.deleteUserAddressByIds(addressIds);
    }
    
    /**
     * 设置默认地址
     *
     * @param addressId 地址ID
     * @return 结果
     */
    @Override
    @Transactional
    public int setDefaultAddress(Long addressId) {
        // 1. 查询该地址信息
        WxUserAddress userAddress = userAddressMapper.selectUserAddressById(addressId);
        if (userAddress == null) {
            return 0;
        }
        
        // 2. 将该用户下所有地址设为非默认
        userAddressMapper.updateDefaultAddress(userAddress.getOpenid());
        
        // 3. 将当前地址设为默认
        WxUserAddress updateAddress = new WxUserAddress();
        updateAddress.setAddressId(addressId);
        updateAddress.setIsDefault("1");
        updateAddress.setUpdateTime(DateUtils.getNowDate());
        updateAddress.setUpdateBy(SecurityUtils.getUsername());
        
        return userAddressMapper.updateUserAddress(updateAddress);
    }
}
